package com.yqg.manage.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.manage.common.interceptor.SysLog;
import com.yqg.manage.mapper.TermMapper;
import io.swagger.models.auth.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.manage.common.Result;

import com.yqg.manage.service.ITermService;
import com.yqg.manage.entity.Term;

import org.springframework.web.bind.annotation.RestController;

/**
 * TermController 前端控制器
 *
 * @author zxf
 * @since 2025-07-26
 */
@RestController
@RequestMapping("/term")
public class TermController {

    @Resource
    private ITermService termService;

    @Resource
    private TermMapper termMapper;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者更新法律条款")
    public Result save(@RequestBody Term term) {
        Boolean edit = termService.edit(term);
        if (edit == null){
            return Result.error("该条款已存在，无需保存");
        }else {
            return Result.success(edit);
        }
    }

    /**
     * 删除前验证是否有行为使用该条款
     */
    @GetMapping("deleteBeforeVerify")
    public Result deleteBeforeVerify(@RequestParam Long id) {
        Integer b = termMapper.deleteBeforeVerify(id);
        return Result.success(b);
    }
    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    @SysLog("删除法律条款")
    public Result delete(@PathVariable Long id) {
        Term term = termService.getById(id);
        term.setIsDelete("1");
        return Result.success(termService.saveOrUpdate(term));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Term> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<Term> list = termService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(termService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false) String lawType,
                           @RequestParam(required = false) String lawName,
                           @RequestParam(required = false) String article,
                           @RequestParam(required = false) String paragraph,
                           @RequestParam(required = false) String subparagraph,
                           @RequestParam(required = false) String item) {
        pageNum = (pageNum - 1) * pageSize;
        List<Term> termList = termMapper.getDataList(pageNum,pageSize,lawType,lawName,article,paragraph,subparagraph,item);
        Integer count = termMapper.getDataListCount(lawType,lawName,article,paragraph,subparagraph,item);
        Map<String,Object> map = new HashMap<>();
        map.put("records",termList);
        map.put("total",count);
        return Result.success(map);
    }

    @GetMapping("/getLevelData")
    public Result getLevelData(@RequestParam Integer level,
                               @RequestParam(required = false) Long lowsId,
                               @RequestParam(required = false) String article,
                               @RequestParam(required = false) String paragraph,
                               @RequestParam(required = false) String subparagraph,
                               @RequestParam(required = false) String item) {
        Map<String, Object> map = termService.getLevelData(level, lowsId, article, paragraph, subparagraph, item);
        return Result.success(map);
    }

}

